﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.mainmenu {
    import flash.events.*;
    import reprise.events.*;
    import org.robotlegs.core.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.models.world.vos.actors.machines.*;
    import net.wooga.pets.models.world.vos.actors.*;
    import net.wooga.pets.views.components.mainmenu.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.controllers.startup.*;
    import net.wooga.pets.models.world.*;

    public class PatientTabMediator extends Mediator implements IMediator, ITicking {

		[Inject]
        public var tabDisplay:PatientTabDisplay;
		[Inject]
        public var languageModel:LanguageModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var worldModel:WorldModel;
		[Inject]
        public var roomSlotsModel:RoomSlotsModel;
		[Inject]
        public var gameViewModel:GameViewModel;

        override public function onRegister():void{
            this.addPatients();
            eventMap.mapListener(this.tabDisplay, ViewCompEvent.CLICK, this.tabDisplay_click);
            eventMap.mapListener(this.tabDisplay, DisplayEvent.VISIBLE_CHANGED, this.visibleChanged);
            eventMap.mapListener(eventDispatcher, EventConstants.CREATE_PATIENT_VIEW, this.patientViewCreated);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_HOSPITAL_LEFT, this.onHospitalLeft);
            eventMap.mapListener(eventDispatcher, EventConstants.DISABLE_PATIENT_TAB, this.disablePatientTab);
            eventMap.mapListener(eventDispatcher, EventConstants.ENABLE_PATIENT_TAB, this.enablePatientTab);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_TREATMENT_STARTED, this.onTreatmentStarted);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_TREATMENT_FINISHED, this.onTreatmentFinished);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_TREATMENT_FINALIZED, this.onTreatmentFinalized);
            dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
        }
        private function visibleChanged(_arg1:DisplayEvent):void{
            this.onTick();
        }
        public function onTick(_arg1:Boolean=true, _arg2:Number=0):void{
            var _local4:IActing;
            var _local5:IRoom;
            var _local6:TreatmentMachineVO;
            var _local7:int;
            if (this.tabDisplay.isHidden()){
                return;
            };
            var _local3:Array = this.worldModel.patients;
            for each (_local4 in _local3) {
                _local5 = IRoom(this.worldModel.getRoomById(_local4.roomId));
                _local6 = (_local5.machine as TreatmentMachineVO);
                _local7 = (_local6) ? (_local6.treatmentTimeLeft - 1) : 0;
                (((_local7 > 0)) && (this.tabDisplay.setTreatmentTimeLeft(_local4.id, TimeUtils.secondsToHoursMinSec(_local7))));
            };
        }
        private function onTreatmentFinalized(_arg1:InfoEvent):void{
            var _local2:String = _arg1.id;
            this.tabDisplay.hideReady(_local2);
        }
        private function onTreatmentFinished(_arg1:InfoEvent):void{
            dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
            var _local2:String = _arg1.id;
            this.tabDisplay.showReady(_local2);
        }
        private function onTreatmentStarted(_arg1:InfoEvent):void{
            dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
            var _local2:PatientVO = PatientVO(_arg1.info);
            this.tabDisplay.hideReady(_local2.id);
            this.tabDisplay.highlightCurrentTreatment(_local2.id, _local2.currentTreatment.roomType);
        }
        private function enablePatientTab(_arg1:Event):void{
            eventMap.mapListener(eventDispatcher, EventConstants.CREATE_PATIENT_VIEW, this.patientViewCreated);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_TREATMENT_STARTED, this.onTreatmentStarted);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_TREATMENT_FINISHED, this.onTreatmentFinished);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_TREATMENT_FINALIZED, this.onTreatmentFinalized);
        }
        private function disablePatientTab(_arg1:Event):void{
            eventMap.unmapListener(eventDispatcher, EventConstants.CREATE_PATIENT_VIEW, this.patientViewCreated);
            eventMap.unmapListener(eventDispatcher, EventConstants.ON_TREATMENT_STARTED, this.onTreatmentStarted);
            eventMap.unmapListener(eventDispatcher, EventConstants.ON_TREATMENT_FINISHED, this.onTreatmentFinished);
            eventMap.unmapListener(eventDispatcher, EventConstants.ON_TREATMENT_FINALIZED, this.onTreatmentFinalized);
        }
        private function tabDisplay_click(_arg1:ViewCompEvent):void{
            var _local2:String = _arg1.sender;
            var _local3:IRoom = IRoom(this.worldModel.getRoomOfActor(_local2));
            var _local4:Slot = this.roomSlotsModel.getSlotById(_local3.slotId);
            if (!_local4.isWaitingSlot()){
                dispatch(new InfoEvent(EventConstants.SHOW_ROOM, String(_local4.id)));
                dispatch(new InfoEvent(EventConstants.HIGHLIGHT_ACTOR, _local2));
            };
        }
        private function patientViewCreated(_arg1:InfoEvent):void{
            var _local2:PatientVO = PatientVO(_arg1.info);
            var _local3:Boolean = (_local2.currentTreatment) ? _local2.currentTreatment.isFinished : true;
            this.addPatient(_local2.id, _local2.type, _local2.treatments, _local3, _local2.diseaseName);
        }
        private function addPatients():void{
            var _local3:PatientVO;
            var _local4:Boolean;
            var _local1:Array = this.worldModel.getPatientsWithoutBabies();
            var _local2:uint = _local1.length;
            if (_local2 == 0){
                this.tabDisplay.addPlaceholders();
                return;
            };
            for each (_local3 in _local1) {
                _local4 = (_local3.currentTreatment) ? _local3.currentTreatment.isFinished : true;
                this.addPatient(_local3.id, _local3.type, _local3.treatments, _local4, _local3.diseaseName);
            };
        }
        private function addPatient(_arg1:String, _arg2:String, _arg3:Array, _arg4:Boolean, _arg5:String):void{
            var _local6:String = ("pet_" + _arg2.toLowerCase());
            var _local7:String = this.languageModel.getTextByKey(_local6);
            this.tabDisplay.addPatient(_arg1, _local7, _arg2, _arg3, this.languageModel.getTextByKey("pets_petsbox_needed_rooms"), this.languageModel.getTextByKey("pets_petsbox_ready"), _arg5);
            if (_arg4){
                this.tabDisplay.showReady(_arg1);
            };
        }
        private function onHospitalLeft(_arg1:InfoEvent):void{
            var _local2:String = _arg1.id;
            this.tabDisplay.removePatientById(_local2);
        }
        override public function preRemove():void{
            dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
        }

    }
}//package net.wooga.pets.views.mainmenu 
